/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx.db;

import cn.webrx.mapper.BookMapper;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.Reader;
import java.util.List;

/**
 * <p>Project: mybatisdemo - DbUtils
 * <p>Powered by webrx On 2021-04-08 16:45:18
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 15
 */
public class DbUtils {
    public static void main(String[] args) throws IOException {
        //配置方式
        //resources mybatis-config.xml
        String res = "mybatis-config.xml";
        Reader reader = Resources.getResourceAsReader(res);
        SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(reader);
        SqlSession session = sf.openSession();
        BookMapper bookdao = session.getMapper(BookMapper.class);
        List<String> list = bookdao.tables();
        System.out.println(list);
        System.out.println(bookdao.query());
    }

    public static void one(String[] args) {
        var tf = new JdbcTransactionFactory();
        var ds = new DruidDataSource();
        ds.setUsername("root");
        ds.setUrl("jdbc:mysql:/likeshop");
        var env = new Environment("development", tf, ds);
        Configuration cfg = new Configuration(env);
        cfg.addMappers("cn.webrx.mapper");

        // cfg.addMapper(BookDao.class);
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(cfg);
        SqlSession session = factory.openSession();

        BookMapper bookdao = session.getMapper(BookMapper.class);
        //System.out.println(bookdao.query());
        System.out.println(bookdao.tables());
    }
}
